/*
 * Copyright 2013 Sigurd Randoll.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.digiway.rapidbreeze.client.config;

import de.digiway.rapidbreeze.shared.rest.ResourceLocation;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

/**
 *
 * @author Sigurd
 */
public class ClientConfiguration {

    private static ClientConfiguration instance;
    private Properties properties;
    private static Path configPath;
    private static final String CONFIG_FILE = "rapidbreeze.config";
    private static final String CONFIG_SERVER_NAME = "Server_Name";
    private static final String CONFIG_SERVER_PORT = "Server_Port";
    private static final String CONFIG_SERVER_USERNAME = "Server_Username";
    private static final String CONFIG_SERVER_PASSWORD = "Server_Password";
    private static final String CONFIG_SERVER_POLLING_INTERVAL = "Server_PollingInterval";

    public static ClientConfiguration getInstance() {
        if (instance == null) {
            instance = new ClientConfiguration();
        }
        return instance;
    }

    private static Path getConfigurationPath() {
        if (configPath == null) {
            configPath = Paths.get(getHome().toString(), CONFIG_FILE);
        }
        return configPath;
    }

    private static Path getHome() {
        String home = System.getProperty("user.home");
        if (home != null && !home.isEmpty()) {
            try {
                Path path = Paths.get(home, ".rapidbreeze");
                if (Files.exists(path) && Files.isDirectory(path)) {
                    return path;
                }
                return Files.createDirectories(path);
            } catch (IOException ex) {
                throw new IllegalStateException("Cannot find configuration directory.", ex);
            }
        }
        throw new IllegalStateException("Cannot find user home directory.");
    }

    ClientConfiguration() {
        properties = new Properties();
        if (Files.exists(getConfigurationPath())) {
            try (InputStream is = Files.newInputStream(getConfigurationPath())) {
                properties.load(is);
            } catch (IOException ex) {
                throw new IllegalStateException("Cannot load property map.", ex);
            }
        }
    }

    public String getServerName() {
        return properties.getProperty(CONFIG_SERVER_NAME, "localhost");
    }

    public int getServerPort() {
        try {
            return Integer.valueOf(properties.getProperty(CONFIG_SERVER_PORT, String.valueOf(ResourceLocation.PORT)));
        } catch (RuntimeException ex) {
            return Integer.valueOf(ResourceLocation.PORT);
        }
    }

    public String getServerUsername() {
        return properties.getProperty(CONFIG_SERVER_USERNAME, "");
    }

    public String getServerPassword() {
        return properties.getProperty(CONFIG_SERVER_PASSWORD, "");
    }

    /**
     * Returns the overall polling interval used for various server polling
     * methods.
     *
     * @return interval in ms
     */
    public int getServerPollingInterval() {
        try {
            return Integer.valueOf(properties.getProperty(CONFIG_SERVER_POLLING_INTERVAL, "500"));
        } catch (RuntimeException ex) {
            return 100;
        }
    }
}
